package com.share.controller;

import com.share.pojo.Dog;
import com.share.pojo.User;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/*

1、json所需内容：bean（可有可无）：class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" /  class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"
            jar包：jackson-databind
    注：有 @ResponseBody 时，返回的是对象的json(jsonView)串；没有 @ResponseBody 时，返回的是xxx.jsp(jspView)
    注：想要Controller返回的是jsonView而不是jspView，用： @ResponseBody  或  ResponseEntity<T>
 */
@Controller
@RequestMapping("/json")
public class JsonController {

    /*
    http://localhost:8080/json/26
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public User getUser(@PathVariable(value = "id") Integer id) {
        User user = new User();
        user.setUsername("sunxj");
        user.setMypassword("123123");
        user.setGender("男");
        return user;
    }

    /*
    请求：http://localhost:8080/sunxj-springmvc1/json/dog
    返回：{"name":"sunxj","color":"black"}
     */
    @RequestMapping(value = "/dog", method = RequestMethod.GET)
    @ResponseBody
    public Dog getDog() {
        Dog dog = new Dog();
        dog.setName("sunxj");
        dog.setColor("black");
        return  dog;
    }

    /*
    http://localhost:8080/json/jsontype/26
     */
    @RequestMapping(value = "/jsontype/{id}", method = RequestMethod.GET)
    public ResponseEntity<User> getUser1(@PathVariable(value = "id") Integer id) throws Exception{
        User user = new User();
        user.setUsername("sunxj");
        user.setMypassword("123123");
        user.setGender("男");
        return new ResponseEntity<User>(user, HttpStatus.OK);
    }
}
